package com.mlingdu.ffmpegaudioplayer.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.mlingdu.ffmpegaudioplayer.R;
import com.mlingdu.ffmpegaudioplayer.common.jni.FFmpegMediaPlayer;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by daimaren on 2017/12/16
 * 975808394@qq.com
 * 使用SDL2 FFmpeg OpenSL ES 播放网络视频流
 */

public class FFmpegMediaPlayerActivity extends AppCompatActivity {

	@BindView(R.id.uri)
	EditText mUrl;
	@BindView(R.id.go_button)
	Button mPlay;
	@BindView(R.id.surfaceview)
	SurfaceView mSurfaceView;

	private SurfaceHolder mSurfaceHolder;
	private Surface mFinalSurface;
	private FFmpegMediaPlayer mMediaPlayer;
	private static final String VIDEO_URL = "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_video_player);
		ButterKnife.bind(this);
		initView();
		initListener();
	}

	private void initView(){
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		mUrl.setText(VIDEO_URL);
		mSurfaceHolder = mSurfaceView.getHolder();
		mMediaPlayer = new FFmpegMediaPlayer();
		mMediaPlayer.setOnPreparedListener(mOnPreparedListener);
		mMediaPlayer.setOnErrorListener(mOnErrorListener);
		mMediaPlayer.stop();
	}

	private void initListener(){
		mPlay.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					mMediaPlayer.reset();
					mMediaPlayer.setDataSource(mUrl.getText().toString());
					if (mFinalSurface != null) {
						mMediaPlayer.setSurface(mFinalSurface);
					}
					mMediaPlayer.prepareAsync();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		});
		mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {
			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				mFinalSurface = holder.getSurface();
			}

			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
				Log.v("TAG", "surfaceChanged format=" + format + ", width=" + width + ", height="
						+ height);
			}

			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {

			}
		});
	}

	FFmpegMediaPlayer.OnPreparedListener mOnPreparedListener = new FFmpegMediaPlayer.OnPreparedListener() {
		@Override
		public void onPrepared(FFmpegMediaPlayer mp) {
			mp.start();
		}
	};

	FFmpegMediaPlayer.OnErrorListener mOnErrorListener = new FFmpegMediaPlayer.OnErrorListener() {
		@Override
		public boolean onError(FFmpegMediaPlayer mp, int what, int extra) {
			Log.d(VideoPlayerActivity.class.getName(), "Error: " + what);
			return true;
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mMediaPlayer != null) {
			mMediaPlayer.stop();
			mMediaPlayer.release();
		}
	}
}
